/**
 * layout组
 */
var CustomLayoutGroup = ccui.Layout.extend({
    /**
     * 类型
     * @public
     */
    type: 0,
    /**
     * 对齐规则
     * @public
     */
    align: 0,
    /**
     * 间隔
     * @public
     */
    spacing: 0,

    onEnterTransitionDidFinish: function () {
        ccui.Layout.prototype.onEnterTransitionDidFinish.call(this);
        this.addHandles();
        this.OnChange();
    },
    /**
     * 添加事件
     */
    addHandles: function () {
        var items = this.getChildren();
        for (var i = 0; i < items.length; i++) {
            var item = items[i];
            if (_.isFunction(item.addHandle)) {
                item.addHandle(this.OnChange.bind(this));
            }
        }
    },
    onExit: function () {
        ccui.Layout.prototype.onExit.call(this);
    },
    /**
     * 改变的运算
     * @private
     */
    OnChange: function () {
        var x = this.x;
        var y = this.y;
        var anchorX = this.getAnchorPoint().x;
        var anchorY = this.getAnchorPoint().y;
        var width = this.getContentSize().width;
        var height = this.getContentSize().height;

        var originX = 0;
        var originY = 0;

        if ((this.align & CreateRoomLayoutGroupAlign.Up) > 0) {
            originY = y + anchorX * height;
        }
        if ((this.align & CreateRoomLayoutGroupAlign.Low) > 0) {
            originY = y - anchorX * height;
        }
        if ((this.align & CreateRoomLayoutGroupAlign.Left) > 0) {
            originX = x - anchorX * width;
        }
        if ((this.align & CreateRoomLayoutGroupAlign.Right) > 0) {
            originX = x + anchorX * width;
        }
        if ((this.align & CreateRoomLayoutGroupAlign.Center) > 0) {
            originX = x + (0.5 - anchorX) * width;
        }
        if ((this.align & CreateRoomLayoutGroupAlign.Middle) > 0) {
            originY = y + (0.5 - anchorY) * height;
        }

        var items = this.getChildren();
        var childX = originX;
        var childY = originY;
        for (var i = 0; i < items.length; i++) {
            var item = items[i];
            if (!item.isVisible()) {
                continue;
            }
            var itemAnchorX = item.getAnchorPoint().x;
            var itemAnchorY = item.getAnchorPoint().y;
            var itemWidth = item.getContentSize().width;
            var itemHeight = item.getContentSize().height;

            if ((this.align & CreateRoomLayoutGroupAlign.Up) > 0) {
                item.y = childY - itemAnchorY * itemHeight;
            }
            if ((this.align & CreateRoomLayoutGroupAlign.Low) > 0) {
                item.y = childY + itemAnchorY * itemHeight;
            }
            if ((this.align & CreateRoomLayoutGroupAlign.Left) > 0) {
                item.x = childX + itemAnchorX * itemWidth;
            }
            if ((this.align & CreateRoomLayoutGroupAlign.Right) > 0) {
                item.x = childX - itemAnchorX * itemWidth;
            }
            if ((this.align & CreateRoomLayoutGroupAlign.Center) > 0) {
                item.x = childX;
            }
            if ((this.align & CreateRoomLayoutGroupAlign.Middle) > 0) {
                item.y = childY;
            }

            if (this.type == CreateRoomLayoutGroupType.LINEAR_VERTICAL) {
                childY -= item.getContentSize().height + this.spacing;
            } else if (this.type == CreateRoomLayoutGroupType.LINEAR_HORIZONTAL) {
                childX += item.getContentSize().width + this.spacing;
            }
        }
    }
});

/**
 * 类型
 * @type {Object}
 */
var CreateRoomLayoutGroupType = {
    LINEAR_VERTICAL: 0,
    LINEAR_HORIZONTAL: 1
};

/**
 * 对齐
 * @type {Object}
 */
var CreateRoomLayoutGroupAlign = {
    Up: 1,
    Low: 2,
    Left: 4,
    Right: 8,
    Center: 16,
    Middle: 32
};